package leetcode.editor.cn;

import utils.ListNode;

/**
 * 两数相加
 * @author qulq
 * @date 2025-02-24 21:44:57
 */
public class T2_AddTwoNumbers{
	 public static void main(String[] args) {
	 	 // 测试代码
	 	 Solution solution = new T2_AddTwoNumbers().new Solution();
	 }
	 
// 力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
		int total = l1.val + l2.val;
		int carry = total / 10;
		ListNode res = new ListNode(total % 10);
		if (l1.next != null || l2.next != null || carry != 0) {
			if (l1.next == null) {
				l1.next = new ListNode(0);
			}
			if (l2.next == null) {
				l2.next = new ListNode(0);
			}
			l1.next.val += carry;
			res.next = addTwoNumbers(l1.next, l2.next);
		}
		return res;
	}
}
//leetcode submit region end(Prohibit modification and deletion)

}